package com.yd;

import com.doman.*;
import com.resultmsg.BaseEnum;
import com.resultmsg.BaseEnums;
import com.resultmsg.Result;
import com.resultmsg.Results;
import com.server.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RequestMapping(value = "/educloud")
@RestController
public class WxLoginController {

    @Autowired
    private TeacherParentMapper teacherParentMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ParentMapper parentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private BaseConfigMapper baseConfigMapper;
    @Autowired
    private TenantMapper tenantMapper;


    @RequestMapping("/text")
    public Result text() {
        return Results.successWithData("1.5",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }


    @RequestMapping("/teacherparentSelectPingTai")
    public Result teacherparentPingTai(TeacherParent teacherParent) {
        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        if (teacherParent1 != null) {
            return Results.successWithData(teacherParent1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }


    @RequestMapping("/teacherparentSelectParents")
    public Result teacherparentParents(TeacherParent teacherParent) {
        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        if (teacherParent1 != null) {
            if(teacherParent1.getPlatforms().equals("2")){
                Parent parent = new Parent();
                parent.setOpenid(teacherParent1.getOpenid());
                Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
                return Results.successWithData(parent1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            }else{
                Teacher teacher = new Teacher();
                teacher.setOpenid(teacherParent1.getOpenid());
                List<Teacher> teacher1 = teacherMapper.selectByPrimaryByOpenid(teacher);
                if (CollectionUtils.isNotEmpty(teacher1)) {
                    return Results.successWithData(teacher1.get(0), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
                }
                return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());

            }
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }




    @RequestMapping("/teacherparentSelect")
    public Result teacherparent(TeacherParent teacherParent) {
        if (teacherParent.getOpenid().isEmpty() || teacherParent.getOpenid().equals("undefined") || teacherParent.getOpenid().equals("")){
            return Results.successWithData("","1002", "错误码：1002，未检测到您的信息，请重新打开！！！");
        }
        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        if (teacherParent1 != null) {
            if(teacherParent1.getPlatforms().equals("2")){
                Parent parent = new Parent();
                parent.setOpenid(teacherParent1.getOpenid());
                Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
                Student student = new Student();
                student.setBindcode(parent1.getCurrentcode());
                Student student1 = studentMapper.selectByPrimaryByCurrentCode(student);
                BaseConfig baseConfig = new BaseConfig();
                baseConfig.setKcode(student1.getGradeno());
                BaseConfig baseConfig1 = baseConfigMapper.selectByPrimaryByKcode(baseConfig);
                student1.setClassno(baseConfig1.getLabel()+student1.getClassno());
                return Results.successWithData(student1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            }else{
                Teacher teacher = new Teacher();
                teacher.setOpenid(teacherParent1.getOpenid());
                List<Teacher> teacher1 = teacherMapper.selectByPrimaryByOpenid(teacher);
                if (CollectionUtils.isNotEmpty(teacher1) && teacher1.size()>1){
                    for (Teacher teacher2 : teacher1) {
                        Tenant tenant = new Tenant();
                        tenant.setTenantId(teacher2.getOrganize());
                        Tenant tenant1 = tenantMapper.selectByPrimaryByOrganize(tenant);
                        teacher2.setOrganizeName(tenant1.getTenantName());
                    }

                    return Results.successWithData(teacher1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
                } else{
                    return Results.successWithData(teacher1.get(0), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
                }

            }
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }



    @RequestMapping("/teacherparentKcode")
    public Result teacherparentKcode(TeacherParent teacherParent) {
        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        if (teacherParent1 != null) {
            if(teacherParent1.getPlatforms().equals("2")){
                Parent parent = new Parent();
                parent.setOpenid(teacherParent1.getOpenid());
                Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
                Student student = new Student();
                student.setBindcode(parent1.getCurrentcode());
                Student student1 = studentMapper.selectByPrimaryByCurrentCode(student);
                Classroom classroom = new Classroom();
                classroom.setKcode(student1.getClassno());
                Classroom classroom1 = classroomMapper.selectByPrimaryKcode(classroom);
                return Results.successWithData(classroom1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            }else{
                Teacher teacher = new Teacher();
                teacher.setOpenid(teacherParent1.getOpenid());
                List<Teacher>  teacher1 = teacherMapper.selectByPrimaryByOpenid(teacher);
                Classroom classroom = new Classroom();
                classroom.setKcode(teacher1.get(0).getClasscode());
                Classroom classroom1 = classroomMapper.selectByPrimaryKcode(classroom);
                return Results.successWithData(classroom1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            }
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }

    @RequestMapping("/teacherparentOpenidAllStudent")
    public Result teacherparentOpenidAllStudent(TeacherParent teacherParent) {
        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        if (teacherParent1 != null) {
            if(teacherParent1.getPlatforms().equals("2")){
                Parent parent = new Parent();
                parent.setOpenid(teacherParent1.getOpenid());
                Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
                String[] split = parent1.getBindcode().split(",");
                List<Student> students2 = new ArrayList<>();
                for (String s : split) {
                    Student student = new Student();
                    student.setBindcode(s);
                    Student student1 = studentMapper.selectByPrimaryByCurrentCode(student);
                    students2.add(student1);
                }
                return Results.successWithData(students2, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            }
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }



    @RequestMapping("/teacherparent")
    public Result addteacherparent(String bindcode,String studentname,String parentname,String parentid,String tel,String openid,String platforms,String teacherbindcode,String teachername,String teachertel) {
        if (openid.isEmpty() || openid.equals("undefined") || openid.equals("")){
            return Results.successWithData("","1002", "错误码：1002，未检测到您的信息，请重新打开！！！");
        }
        if (platforms.equals("2")){
            Student student = new Student();
            student.setBindcode(bindcode);//绑定码
            student.setName(studentname);//学生姓名
            Student studertService =  studentMapper.selectByPrimaryBindCodeAndName(student);
            if (studertService == null) {
                System.out.println("没有找到学生信息，绑定码为"+bindcode+"学生姓名为"+studentname);
                return Results.successWithData("","1003", "绑定码或学生姓名错误");
            }
            String[] split1 = studertService.getRoleParents().split(",");
            for (int i = 0; i < split1.length; ++i){
                if (split1[i].equals(openid)){
                    return Results.successWithData("","1003", "请勿重复绑定");
                }
            }
            for (int i = 0; i < split1.length; ++i){
                if(i == Integer.valueOf(parentid)-1){
                    if (!split1[i].equals("0")){
                        return Results.successWithData("","1003", "此家长身份已被绑定");
                    }
                }
            }
            if (studertService != null){
                TeacherParent teacherParent = new TeacherParent();
                teacherParent.setOpenid(openid);
                TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
                if (teacherParent1 != null){
                    teacherParent1.setPlatforms(platforms);
                    int teacherParent2 = teacherParentMapper.updateByPrimaryByOpenid(teacherParent1);
                }else{
                    teacherParent.setPlatforms(platforms);
                    int insertSelective = teacherParentMapper.insertSelective(teacherParent);
                }
                Parent parent = new Parent();
                parent.setOpenid(openid);
                Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
                if(parent1 != null){
                    String[] split = parent1.getBindcode().split(",");
                    for (String s : split) {
                        if (s.equals(bindcode)){
                            return Results.successWithData("","30000", "请勿重复添加");
                        }
                    }
                    parent1.setBindcode(parent1.getBindcode()+","+ bindcode);
                    parent1.setCurrentcode(bindcode);
                    int i = parentMapper.updateByPrimaryByOpenid(parent1);
                }else{
                    parent.setCurrentcode(bindcode);//学生当前绑定码
                    parent.setBindcode(bindcode);//学生编号数组
                    parent.setName(parentname);//家长名称
                    parent.setTel(tel);//家长手机号
                    parent.setParentid(Integer.valueOf(parentid));//家长类型
                    parent.setCreateTime(new Date());
                    int insertSelective1 = parentMapper.insertSelective(parent);
                }
                String str = "";
                String[] strArr = studertService.getRoleParents().split(",");
                for (int i = 0; i < strArr.length; ++i){
                    if(i == Integer.valueOf(parentid)-1){
                        str += openid+",";
                    }else{
                        str += strArr[i]+",";
                    }
                }
                studertService.setRoleParents(str);
                if(studertService.getBindStatus() == 0){
                    studertService.setBindStatus(1);
                }
                int updateByPrimaryKeySelective = studentMapper.updateByPrimaryKeySelective(studertService);
                return Results.successWithData(studertService,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
            }else{
                return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
            }
        }else  if (platforms.equals("1")){
            TeacherParent teacherParent = new TeacherParent();
            teacherParent.setOpenid(openid);
            TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
            Teacher teacher = new Teacher();
            teacher.setBindcode(teacherbindcode);
            teacher.setName(teachername);
            Teacher insertSelective1 = teacherMapper.selectByPrimaryBindcodeAndTeacherName(teacher);
            if (insertSelective1 != null){
                if(insertSelective1.getOpenid() != null){
                    return Results.successWithData("","30000","该老师已被绑定");
                }
                if (teacherParent1 != null){
                    teacherParent1.setPlatforms(platforms);
                    int teacherParent2 = teacherParentMapper.updateByPrimaryByOpenid(teacherParent1);
                }else{
                    teacherParent.setPlatforms(platforms);
                    int insertSelective = teacherParentMapper.insertSelective(teacherParent);
                }
                if(insertSelective1.getBindStatus() == 0){
                    insertSelective1.setBindStatus(1);
                }
                insertSelective1.setOpenid(openid);
                int updateByPrimaryKeySelective = teacherMapper.updateByPrimaryByCodeUpdate(insertSelective1);
                if(updateByPrimaryKeySelective > 0){
                    return Results.successWithData(insertSelective1,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
                }
            }
        }
        return Results.successWithData("",BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
    }



}
